// pages/chatView/chatView.js
import { getTokenValue, setUserInfo } from '../../utils/storage'
import { personInfo, getUserInfoFromMobile, getCodeByPhone } from '../../api/index'
import { webUrl } from '../../utils/request'
import { trackingEventMap, trackingEvent } from '../../utils/tracking'
const globalData = getApp().globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlSrc: '',
    shareTag: '',
    inviteQuery: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // let url = 'http://172.16.10.166:8088/chat'
    let url = webUrl + '/chat'
    if (options.redirectUrl && options.redirectUrl !== 'undefined' && options.redirectUrl !== 'null') {
      url = webUrl + decodeURIComponent(options.redirectUrl)
    }
    let queryStrArr = ['wxSource=bpai']
    // 查询参数
    if (getTokenValue()) {
      queryStrArr.push(`token=${getTokenValue()}`)
    }
    if (options.sourceTag) {
      queryStrArr.push(`sourceTag=${options.sourceTag}`)
    }

    if (queryStrArr.length > 0) {
      let queryStr = queryStrArr.join('&')
      if (url.indexOf('?') > -1) {
        url = `${url}&${queryStr}`
      } else {
        url = `${url}?${queryStr}`
      }
    }
    console.info(url)
    this.setData({
      urlSrc: url
    })

    if (getTokenValue()) {
      this.getPersonInfo()
      this.getUserInfoData()
    }

  },
  async getPersonInfo() {
    let result = await personInfo({ queryType: 2 })
    if (result.inviteUrl) {
      this.setData({
        inviteQuery: result.inviteUrl.split('?')[1]
      })
    }
  },
  // 获取用户信息及分享参数
  async getUserInfoData() {
    let result = await getUserInfoFromMobile()
    setUserInfo(JSON.stringify(result))
    let tagResult = await getCodeByPhone({
      phone: result.phoneNumber
    })
    this.setData({
      shareTag: tagResult || ''
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.hideHomeButton();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage() {
    trackingEvent(trackingEventMap.shareForward)
    globalData.showSwitch = '0'
    let inviteQuery = this.data.inviteQuery
    let shareTag = this.data.shareTag
    let sharePath = globalData.activityConfig.sharePath
    let imageUrl = 'https://psy-public.obs.myhuaweicloud.com/bpai/active/20240331/act006.png'
    let t = Date.now()

    if (globalData.activityConfig.activitySwitch == '1') {
      imageUrl = 'https://psy-public.obs.myhuaweicloud.com/bpai/active/20240520/act005.png'
    }
    if (inviteQuery) {
      if (shareTag) {
        inviteQuery = inviteQuery + `&sourceTag=` + shareTag
      }
      return {
        title: globalData.activityConfig.shareTitle,
        path: `${sharePath}?${inviteQuery}`,
        imageUrl: `${imageUrl}?t=${t}`
      }
    } else {
      return {
        title: '分享标题参数缺少，请登录后分享',
        path: `${sharePath}`,
        imageUrl: `${imageUrl}?t=${t}`
      }
    }
  }
})